//
//  LSManyPeopleListVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/10/9.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LSManyPeopleListVC.h"
#import "addNewFriendDetailVC.h"
#import "LSManyPeopleChatVC.h"

@interface LSManyPeopleListVC ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong ,nonatomic)NSMutableArray * peopleListArr;//用户数组
@property (strong, nonatomic)NSMutableArray * imgArr;//头像数组
@property (strong, nonatomic)NSMutableArray * btnArr;//按钮数组
@property (strong, nonatomic)NSMutableArray * labelArr;//名称数组
@end

@implementation LSManyPeopleListVC

#pragma mark - 初始化
-(NSMutableArray *)imgArr{

    if (!_imgArr) {
        _imgArr = [NSMutableArray array];
    }
    return _imgArr;

}

-(NSMutableArray *)btnArr{

    if (!_btnArr) {
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;

}

-(NSMutableArray *)labelArr{

    if (!_labelArr) {
        _labelArr = [NSMutableArray array];
    }
    return _labelArr;

}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];
    
    [self sendRequest];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"讨论组成员";
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //获取讨论组成员列表
//    [[RCIMClient sharedRCIMClient] getDiscussion:self.tlzDict[@"discussionId"] success:^(RCDiscussion *discussion) {
//        self.peopleListArr = [discussion.memberIdList copy];
//        DLog(@"讨论组成员:%@",self.peopleListArr);
//    } error:^(RCErrorCode status) {
//        DLog(@"create discussion Failed &gt; %ld!", (long)status);
//    }];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

-(void)sendRequest{

    NSMutableDictionary * dictData = [NSMutableDictionary dictionary];
    
    // 批量请求成员信息
    
    [dictData setValue:allConstMemberToken forKey:@"token"];
    
    if (self.tlzDict) {
        
        [dictData setValue:self.tlzDict[@"discussionId"] forKey:@"groupId"];
        
    }else{
    
         [dictData setValue:self.discussionId forKey:@"groupId"];
    
    }
    
    NSString *str = @"getCircleMember_app_social.im.lensum?";
    
    [LSHttpTool sendHttpData:str params:dictData success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            self.peopleListArr = dicInfo[@"data"];
            
            NSInteger  count;
            if (self.discussionId) {
                
                count = self.peopleListArr.count ;
                
            }else{
            
                count = self.peopleListArr.count - 1;
            
            }
            
            for (int i = 0; i <= count; i++) {
                
                NSDictionary * dict = [NSDictionary dictionary];
                
                if (i != self.peopleListArr.count) {
                    
                    dict = self.peopleListArr[i];
                    
                }
                
                UIButton *labelBtn = [[UIButton alloc] init];
                labelBtn.height = screenW * 0.15;
                labelBtn.width = labelBtn.height;
                
                labelBtn.x = i%5*((screenW- 20)/5)+18;
                labelBtn.y = i/5*(labelBtn.height * 1.4) + 10;
                labelBtn.layer.masksToBounds = YES;
                labelBtn.layer.borderColor = [UIColor whiteColor].CGColor;
                [labelBtn setTitleColor:RGB(137, 137, 137) forState:UIControlStateNormal];
                labelBtn.layer.borderWidth = 1.0f;
                labelBtn.layer.cornerRadius = 5.0f;
                labelBtn.tag = i;
                [labelBtn addTarget:self action:@selector(clickPhoto:) forControlEvents:UIControlEventTouchUpInside];
                
                UIImageView * imgView = [[UIImageView alloc]init];
                imgView.frame = labelBtn.frame;
                
                UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(labelBtn.x, labelBtn.y + labelBtn.height + 5, labelBtn.width, 14)];
                nameLabel.font = [UIFont systemFontOfSize:10.f];
                nameLabel.textAlignment = NSTextAlignmentCenter;
                
                if (i == self.peopleListArr.count && self.discussionId) {
                    
                    imgView.image = [UIImage imageNamed:@"btn_lingdao"];
                    nameLabel.text = @"添加成员";
                    nameLabel.textColor = [UIColor lightGrayColor];//管理员界面
                    
                }else{
                
                    [imgView sd_setImageWithURL:[NSURL URLWithString:dict[@"headPhoto"]]];
                    nameLabel.text = dict[@"userName"];
                    nameLabel.textColor = [UIColor blackColor];//普通成员界面
                
                }
                
                [self.scrollView addSubview:nameLabel];
                [self.scrollView addSubview:imgView];
                [self.scrollView addSubview:labelBtn];
                
                [self.imgArr addObject:imgView];
                [self.btnArr addObject:labelBtn];
                [self.labelArr addObject:nameLabel];
            }
            
        }
        
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];
        
    }];

}

-(void)leftBarButtonItemClick{

    [self.navigationController popViewControllerAnimated:YES];

}

#pragma mark - 点击头像

-(void)clickPhoto:(UIButton *)btn{

//    NSLog(@"点击头像%ld",(long)btn.tag);
    
    if (btn.tag == self.peopleListArr.count) {
        
        LSManyPeopleChatVC * LSManyPeopleChat = [[LSManyPeopleChatVC alloc]init];
        LSManyPeopleChat.tlzCYArr = self.peopleListArr;
        LSManyPeopleChat.tlzStr = self.discussionId;
        
        [self.navigationController pushViewController:LSManyPeopleChat animated:YES];//添加成员
        
    }else if([self.tlzDict[@"creatorId"] isEqualToString:[USER_DEFAULTS objectForKey:@"account"]]){
    
    addNewFriendDetailVC * addNewFriendDetail = [[addNewFriendDetailVC alloc]init];
    NSDictionary * dict = self.peopleListArr[btn.tag];
    NSString * userId = dict[@"userId"];
    
    if ([allConstMemberid integerValue] != [userId integerValue]) {
        
        addNewFriendDetail.userId = dict[@"userId"];
        addNewFriendDetail.userType = dict[@"userType"];
        addNewFriendDetail.discussionId = self.tlzDict[@"discussionId"];
        addNewFriendDetail.creatorId = self.tlzDict[@"creatorId"];
        addNewFriendDetail.account = dict[@"account"];
        
        [self.navigationController pushViewController:addNewFriendDetail animated:YES];//管理员查看资料
        
    }else{
        
        NSLog(@"点击了自己");
        
    }
        
    }else if (![self.tlzDict[@"creatorId"] isEqualToString:[USER_DEFAULTS objectForKey:@"account"]]){
        
        addNewFriendDetailVC * addNewFriendDetail = [[addNewFriendDetailVC alloc]init];
        NSDictionary * dict = self.peopleListArr[btn.tag];
        NSString * userId = dict[@"userId"];
        
        if ([allConstMemberid integerValue] != [userId integerValue]) {
            
            addNewFriendDetail.userId = dict[@"userId"];
            addNewFriendDetail.userType = dict[@"userType"];
            addNewFriendDetail.discussionId = self.tlzDict[@"discussionId"];
//            addNewFriendDetail.creatorId = self.tlzDict[@"creatorId"];
            addNewFriendDetail.account = dict[@"account"];
            
            [self.navigationController pushViewController:addNewFriendDetail animated:YES];//普通成员查看资料
            
        }else{
            
            NSLog(@"点击了自己");
            
        }
    
    }

}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:YES];
    
    for (UIView *view in [self.scrollView subviews]) {
        [view removeFromSuperview];
    }

}
@end
